#include "View.hpp"
#include "StyleManager.hpp"
#include <QtGui>
#include <QObject>

View::View()
{}

View::~View()
{	del();	}

void View::del()
{
	try
	{	
		delete v_chessboard;
		
		if (v_players[Chess::WHITE] != 0)
		{
			delete v_players[Chess::WHITE];
			v_players[Chess::WHITE] = 0;
		}
		
		if (v_players[Chess::BLACK] != 0)
		{
			delete v_players[Chess::BLACK];
			v_players[Chess::BLACK] = 0;
		}
		
		delete statusbox;
		delete StyleManager::getInstance();
	}
	catch(Exception* e)
	{	throw e;	}
}

void	View::init()
{
	try
	{	
		mainwin.show();
		statusbox = new StatusBox();
		mainwin.getGraphicsView()->setScene(&graphicsscene);
		v_chessboard = new ChessboardView();
		v_players[Chess::WHITE] = new PlayerView(Chess::WHITE);
		v_players[Chess::BLACK] = new PlayerView(Chess::BLACK);
	}
	catch(Exception* e)
	{	throw e;	}
}

void	View::reset()
{
	try
	{
		if (v_players[Chess::WHITE] != 0)
		{
			delete v_players[Chess::WHITE];
			v_players[Chess::WHITE] = new PlayerView(Chess::WHITE);
		}
		else
			v_players[Chess::WHITE] = new PlayerView(Chess::WHITE);
		
		if (v_players[Chess::BLACK] != 0)
		{
			delete v_players[Chess::BLACK];
			v_players[Chess::BLACK] = new PlayerView(Chess::BLACK);
		}
		else
			v_players[Chess::BLACK] = new PlayerView(Chess::BLACK);
	}
	catch(Exception* e)
	{	throw e;	}
}

void	View::refresh()
{
	try
	{
		v_chessboard->refresh();
		v_players[Chess::WHITE]->refresh();
		v_players[Chess::BLACK]->refresh();
		graphicsscene.update();
		mainwin.getGraphicsView()->update();
	}
	catch(Exception* e)
	{	throw e;	}
}


Piece::Type	View::askForPawnPromotion()
{
	try
	{
		Piece::Type		ret = Piece::PAWN;
		
		QMessageBox		msgpromote;
		
		msgpromote.setText("Please choose a piece type to promote the pawn :");
		QAbstractButton*	rookbutton = msgpromote.addButton(QObject::tr("Rook"), QMessageBox::AcceptRole);
		QAbstractButton*	knightbutton = msgpromote.addButton(QObject::tr("Knight"), QMessageBox::AcceptRole);
		QAbstractButton*	bishopbutton = msgpromote.addButton(QObject::tr("Bishop"), QMessageBox::AcceptRole);
		QAbstractButton*	queenbutton = msgpromote.addButton(QObject::tr("Queen"), QMessageBox::AcceptRole);

		msgpromote.exec();
		
		if (msgpromote.clickedButton() == rookbutton)
			ret = (Piece::ROOK);
		else if (msgpromote.clickedButton() == knightbutton)
			ret = (Piece::KNIGHT);
		else if (msgpromote.clickedButton() == bishopbutton)
			ret = (Piece::BISHOP);
		else if (msgpromote.clickedButton() == queenbutton)
			ret = (Piece::QUEEN);
		else
		{
			throw new CriticalException("View::askForPawnPromotion : PawnPromotion msg error !");
		}
		
		return (ret);
	}
	catch(Exception* e)
	{	throw e;	}
}

ChessboardView*		View::getChessboardView() const
{	return (v_chessboard);	}

PlayerView*			View::getPlayerView(Chess::Color color) const
{	return (v_players[color]);	}


QGraphicsScene*		View::getScene()
{	return &graphicsscene;	}

MainWin*		View::getMainWin()
{	return &mainwin;	}

StatusBox*			View::getStatusBox() const
{	return (statusbox);	}
